import { Component, OnInit ,ElementRef} from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router,NavigationExtras } from '@angular/router'; 
import {ActivatedRoute} from '@angular/router'
import {ContractServiceSvr} from '../../services/contractServiceSvr'
import { DatePipe } from '@angular/common';

@Component({
  selector: 'app-sign',
  templateUrl: './sign.component.html',
  styleUrls: ['./sign.component.less']
})
export class SignComponent implements OnInit {
  validateForm!: FormGroup;

  submitForm(): void {
    if (this.validateForm.valid) {
      console.log('submit', this.validateForm.value);
    } else {
      Object.values(this.validateForm.controls).forEach(control => {
        if (control.invalid) {
          control.markAsDirty();
          control.updateValueAndValidity({ onlySelf: true });
        }
      });
    }
  }
  // 是否显示正在加载中
  isSpin:boolean=false
  // 房屋id
  HouseId:any=''
  // 客户id
  custmerID:any=''
  // 合同id
  contractId:string=''
  // 租金
  rent:any
  // 折扣后的租金
  Discounted:any
  // 房屋具体位置
  DetailAddress:string=''
  constructor(
    private fb: FormBuilder,
    private router:Router,
    private contractServiceSvr:ContractServiceSvr,
    private route: ActivatedRoute,
    private el:ElementRef,
    // private datePipe: DatePipe
    ) {
      this.route.queryParams.subscribe((data) => {
        // data是一个对象，里面有传过来的值
           this.HouseId = data['key'];
           this.custmerID = data['custmerId'];
           this.rent=data['rent']
           this.DetailAddress=data['DetailAddress']
        });
    }

  ngOnInit(): void {
    
    this.validateForm = this.fb.group({
      userName: [null, [Validators.required]],
      identityCard: [null, [Validators.required]],
      telephone: [null, [Validators.required]],
      rent: [null, [Validators.required]],
      time:[null,[Validators.required]],
      Methods:[null,[Validators.required]],
      remark:[null,[Validators.required]],
      
    });
    console.log('this.custmerID:'+this.custmerID);
    console.log('this.rent:'+this.rent);
    
      this.contractServiceSvr.getDiscountPrice(this.custmerID,this.rent).then((res=>{
      console.log(res);
      this.Discounted=res
    }))
    // console.log('rent'+this.rent);
  }

  time:string=''
  Methods:string=''
  Day:number=new Date().getDate()
  month:number=new Date().getMonth()+1
  year:number=new Date().getFullYear()
  date:string=this.year+'-'+this.month+'-'+this.Day
  today: Date = new Date();
  Time:number= Number(this.time)
  payPeriod:number=Number(this.Methods)
  remark:string=''
  file:string=''
  // name:string=''
  // 跳转到签约页面
  goContract(){
    this.isSpin=true

    if(this.time==''||this.Methods==''){
      console.log('不能跳转');
    this.isSpin=false
    }else if(this.time==''&&this.Methods==''){
      console.log('不能跳转');
    this.isSpin=false
    }else{
      this.contractServiceSvr.saveContract('',this.custmerID,Number(this.time),this.HouseId,this.date,Number(this.Methods),this.remark).then((res=>{
        console.log('合同');
        // console.log(res);
        this.file=res.Url
        this.contractId=res.Id
        console.log('id:'+this.contractId);
        // console.log('url'+this.file);
        let navigationExtras:NavigationExtras = {
          queryParams:{
            'file':this.file,
            'contractId':this.contractId,
            'rent':this.rent,
            'custmerID':this.custmerID
          }
        }
        this.router.navigate(['/contract'],navigationExtras)
      }))
    }
  console.log('Time'+Number(this.time));
  console.log('custmerID'+this.custmerID);
  console.log('HouseId'+this.HouseId);
  console.log('date'+this.date);
  console.log('remark'+this.remark);
  console.log('payPeriod'+Number(this.Methods));
  // console.log('remark'+this.remark);
  }
  // 
  isNotHaveYear:Boolean=false
  changeTime(){
   if(this.time=='12'){
    this.isNotHaveYear=true
    console.log(this.time);
   }else{
    this.isNotHaveYear=false
   }
  // console.log('111'+this.time);
  
    
  }
}
